package mark.tom.gear.view.activity

import android.Manifest
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import mark.tom.gear.R
import mark.tom.gear.base.activity.BaseActivity
import mark.tom.gear.base.callback.CallBack
import mark.tom.gear.base.extension.showToast
import mark.tom.gear.base.utils.PermissionsUtil
import mark.tom.gear.base.utils.PrivacyUtil
import mark.tom.gear.databinding.ActivitySplashBinding
import mark.tom.gear.vm.SplashViewModel

/**
 * @Description: 开屏页Activity
 * @Author: ttmk110@163.com
 * @Package: mark.tom.gear.view.activity
 * @Date: 2021/1/15
 * @Copyright: 版权归 Mark 所有
 * <ModifyLog>
 * @ModifyContent:
 * @Author:
 * @Date:
 * </ModifyLog>
 */
class SplashActivity : BaseActivity<SplashViewModel>() {

    override fun getLayoutView(inflater: LayoutInflater, mRootView: ViewGroup): View {
        return ActivitySplashBinding.inflate(inflater, mRootView, false).root
    }

    override fun isToolBarInContent(): Boolean {
        return false
    }

    override fun isUseDartTheme(): Boolean {
        return true
    }

    override fun onCreate(contentView: View, savedInstanceState: Bundle?) {
        // 注册GMU初始化结果观察者
        mViewModel.gmuManagerInitResultLiveData.observe(this, {
            if (it.success) {
                afterInitGmu()
            } else {
                it.msg.showToast()
            }
        })
        // 注册GMU Service结果观察者
        mViewModel.gmuServiceInitResultLiveData.observe(this, {
            TODO("继续流程")
        })
        // 初始化GMU
        mViewModel.initGMUManager()
    }

    /**
     * Description: GMU初始化完成后处理流程
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/2/8 4:44 下午
     */
    private fun afterInitGmu() {
        // 如果隐私政策配置且未被打开过
        if (PrivacyUtil.checkPrivacyShow()) {
            // 显示隐私政策页面
            PrivacyUtil.showPrivacy(this)
        }
        // 如果权限提示未显示过
        if (PrivacyUtil.checkPermissionTipShow()) {
            // 如果配置过隐私政策 则需要等政策显示后再显示权限说明
            if (PrivacyUtil.checkPrivacyShow()) {
                Handler(Looper.getMainLooper()).postDelayed({
                    showPermissionTip()
                }, 1000L)
            } else {
                showPermissionTip()
            }
        } else {
            afterPermissionTip()
        }
    }

    /**
     * Description: 显示权限申请说明对话框
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/2/8 4:44 下午
     */
    private fun showPermissionTip() {
        PrivacyUtil.showPermissionTipShow(this, R.layout.hlgb_permission_overlay, object: CallBack<Any, Unit> {
            override fun onSuccess(param: Any) {
                PrivacyUtil.updatePermissionTipReadStatus()
                PrivacyUtil.dismissPermissionTip()
                afterPermissionTip()
            }

            override fun onFail(param: Any) {
                PermissionsUtil.markAllPermissionsDenied(arrayOf(
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ), intArrayOf(-1, -1), this@SplashActivity)
                PrivacyUtil.updatePermissionTipReadStatus()
                PrivacyUtil.dismissPermissionTip()
                afterPermissionTip()
            }
        })
    }

    /**
     * Description: 申请读取手机信息和写入存储权限
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/2/8 4:45 下午
     */
    private fun afterPermissionTip() {
        PermissionsUtil.checkPermission(this, arrayOf(
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ), object: CallBack<Any, Unit> {
            override fun onSuccess(param: Any) {
                afterPermissionCheck()
            }

            override fun onFail(param: Any) {
                afterPermissionCheck()
            }
        })
    }

    /**
     * Description: 权限申请后的GMU服务初始化
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/2/8 5:55 下午
     */
    private fun afterPermissionCheck() {
        mViewModel.initGMUService(this)
    }
}
